import { ElSelect, ElOption } from 'element-plus';
import storeDict from '../../../store/sessionStorageDict/index'
import '../config/index.scss'

const dictData = storeDict()
const Select = (config, params, emit, hasEvent) => {
    if(config.hasOwnProperty('dict')) {
        dictData?.getDict(config.dict)
        return (
            <ElSelect class="select" {...config.labelProps} v-model={params[config.props.prop]} >
                {
                    handleFilterData(dictData.dictData[config.dict], params[config.props.prop]).map(cell => {
                        return (
                            <ElOption key={cell.value} label={cell.label} value={cell.value}></ElOption>
                        )
                    })
                }
            </ElSelect>
        )
    }else {
        if(hasEvent) {
            return <ElSelect class="select" {...config.labelProps} v-model={params[config.props.prop]} onChange={(value) => emit('changeEvent', {...config, value})}>
                {
                    config.options.map(cell => {
                        return (
                            <ElOption key={cell.value} label={cell.label} value={cell.value}></ElOption>
                        )
                    })
                }
            </ElSelect>
        }else {
            return <ElSelect class="select" {...config.labelProps} v-model={params[config.props.prop]} >
                {
                    config.options.map(cell => {
                        return (
                            <ElOption key={cell.value} label={cell.label} value={cell.value}></ElOption>
                        )
                    })
                }
            </ElSelect>
        }
    }
}

// 处理 options 数据
function handleFilterData(dictData, prop) {
    return dictData.map(item => {
        return {
            label: item.dictLabel,
            value: typeof prop === 'string'? item.dictValue.toString(): parseInt(item.dictValue)
        }
    })
}
export default Select
